package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dtos.ModuloDTO;
import dtos.ServiciosModuloDTO;

/**
 * Servlet implementation class EditarModulo
 */
@WebServlet("/EditarModulo")
public class EditarModulo extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private bd.BusinessDelegate bd = null;
	   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditarModulo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		PrintWriter out = response.getWriter();
		String idPasado1 = request.getParameter("idModulo");
		int idPasado = Integer.parseInt(idPasado1);
		String path1 = "";
		String path2 = "";
		String path3 = "";
		
		ModuloDTO mod = bd.getInstance().getModulo(idPasado);
		if(mod!=null){
			ServiciosModuloDTO s1 = bd.getInstance().getServicio(1, mod.getId());
			ServiciosModuloDTO s2 = bd.getInstance().getServicio(2, mod.getId());
			ServiciosModuloDTO s3 = bd.getInstance().getServicio(3, mod.getId());
			
			if(s1!=null){
				path1 = s1.getPath();			
			}
			if(s2!=null){
				path2 = s2.getPath();			
			}
			if(s3!=null){
				path3 = s3.getPath();			
			}
		}
		out.println("<table cellpadding='5' cellspacing='5' border='0' style='border:2px solid #A4E100; width: 95%; padding: 10px; margin: 10px;'>");
		out.println("<tr>");
		out.println("<td colspan='3' style='font-weight:bold; line-height:0.5em;'>EDITAR MODULO ID <input name='edit_id' id='edit_id' value='"+mod.getId()+"' style='background-color: #2D2D2D; font-family: sans-serif; font-size:11px; color: #FFFFFF; border: 0px; font-weight: bold;' readonly></input></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td colspan='3'><hr style='color: #A4E100; border: 2px solid #A4E100;'></hr></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("	<td>Nombre: <input id='nombre' name='nombre' value='"+mod.getNombre()+"' style='margin: 3px; border: solid 2px #A4E100; font-family: sans-serif; font-size:10px; '></input></td>");
		out.println("	<td>IP:  <input id='ip' name='ip' value='"+mod.getIp()+"' style='margin: 3px; border: solid 2px #A4E100; font-family: sans-serif; font-size:10px; '></input></td>");
		out.println("	<td>Activo:  <input id='activo' name='activo' value='"+mod.isActivo() +"' style='margin: 3px; border: solid 2px #A4E100; font-family: sans-serif; font-size:10px; '></input></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td colspan='3'><hr style='color: #A4E100; border: 1px solid #A4E100;'></hr></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("	<td style='width: 45%;' align='center'>Servicios</td>");
		out.println("	<td style='width: 5%;' align='center'>");
		out.println("	</td>");
		out.println("	<td style='width: 45%;' align='center'>Path");
		out.println("	</td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td colspan='5'><hr style='color: #A4E100; border: 1px solid #A4E100;'></hr></td>");
		out.println("</tr>");
		//Primer servicio
		out.println("<tr>");
		out.println("	<td style='width: 45%;'>Servicio 1 = Cola Mensajes</td>");
		out.println("	<td style='width: 5%;' align='center'>-</td>");
		out.println("	<td style='width: 45%;'><input type='text' name='path1' value='"+path1+"'></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("	<td style='width: 45%;'>Servicio 2 = Rest</td>");
		out.println("	<td style='width: 5%;' align='center'>-</td>");
		out.println("	<td style='width: 45%;'><input type='text' name='path2' value='"+path2+"'></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("	<td style='width: 45%;'>Servicio 3 = Webservics</td>");
		out.println("	<td style='width: 5%;' align='center'>-</td>");
		out.println("	<td style='width: 45%;'><input type='text' name='path3' value='"+path3+"'></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("	<td colspan='3'><input style='margin:3px; cursor: pointer; width: 90px;' type='submit' name='guardar' id='guardar' value='Guardar' class='button grey'></input></td>");
		out.println("</tr>");
		out.println("</table>");
	}

}
